﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
 using System.Configuration;
namespace DllSearcher
{
   

    // This is a representation of our web.config, we can change the properties and call save to save them
    public class WebConfigSettings
    {
        // This holds our configuration element so we dont have to reopen the file constantly
        private Configuration config;

        // given a path to a web.config, this ctor will init the class and open the config file so it can map the getters / setters to the values in the config
        public WebConfigSettings(string path)
        {
            // open the config via a method that we wrote, since we'll be opening it in more than 1 location
            this.config = this.OpenConfig(path);
        }

        // Read/Write property that maps to a web.config setting
        public string MySetting
        {
            get { return this.Get("MySetting"); }
            set { this.Set("MySetting", value); }
        }

        // Read/Write property that maps to a web.config setting
        public string MySetting2
        {
            get { return this.Get("MySetting2"); }
            set { this.Set("MySetting2", value); }
        }

        // helper method to get the value of a given key
        public string Get(string key)
        {
            NameObjectCollectionBase.KeysCollection namespaceDeclared = config.Sections.Keys;
            ConfigurationSection configurationSectionGroup = config.GetSection(key);
            var element = config.AppSettings.Settings[key];

            // it may be null if its not there already
            if (element == null)
            {
                // we'll handle it not being there by adding it with the new value
                config.AppSettings.Settings.Add(key, "");

                // pull the element again so we can set it below
                element = config.AppSettings.Settings[key];
            }
            return element.Value;
        }

        // helper method to set the value of a given key
        private void Set(string key, string value)
        {
            // now that we have our config, grab the element out of the settings
            var element = this.config.AppSettings.Settings[key];

            // it may be null if its not there already
            if (element == null)
            {
                // we'll handle it not being there by adding it with the new value
                config.AppSettings.Settings.Add(key, value);
            }
            else
            {
                // in this case, its already present, just update the value
                element.Value = value;
            }
        }

        // Writes all the values to the config file
        public void Save()
        {
            // save the config, minimal is key here if you dont want huge web.config bloat
            this.config.Save(ConfigurationSaveMode.Minimal, true);
        }

        public void SaveAs(string newPath)
        {
            this.config.SaveAs("", ConfigurationSaveMode.Minimal, true);

            // due to some weird .net issue, you have to null the config out after you SaveAs it because next time you try to save, it will error
            this.config = null;
            this.config = this.OpenConfig(newPath);
        }

        // where the magic happens, we'll open the config here     
        protected Configuration OpenConfig(string path)
        {
            return ConfigurationManager.OpenMappedExeConfiguration(
                  new ExeConfigurationFileMap() { ExeConfigFilename = path },
                  ConfigurationUserLevel.None);
        }
    }
}
